﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using System.Text;
using System.Web.UI.HtmlControls;


namespace CRM.Web
{
    public partial class Top : Common.PageBase
    {
        string divBegin = "<div id=\"frmDiv\"  name=\"frmDiv\" style=\" border-color:  #6D93C8; border-style: solid; border-width: 1px; background-color:#e9f2f8; width: 120px; height: {0}px; \"  >";
        string divEnd = "</div>";
        string divRowToNewPage = "<div style=\"cursor:pointer; cursor: hand; height: 25px; line-height: 25px; font-size: 12px; width: 118px;    \" OnMouseOut=\"this.style.backgroundColor=c;\" OnMouseOver=\"c = this.style.backgroundColor; this.style.backgroundColor='#ffffff'; \"  onclick=\"parent.NewOpenUrl('{0}', '850', '600');\" ><span style=\"margin-left: 10px;\">{1}</span></div>";
        string divRowToFrame = "<div style=\"cursor:pointer; cursor: hand; height: 25px; line-height: 25px; font-size: 12px; width: 118px; \" OnMouseOut=\"this.style.backgroundColor=c;\" OnMouseOver=\"c = this.style.backgroundColor; this.style.backgroundColor='#ffffff'; \"  onclick=\"parent.mainFrameOpenUrl('{0}');\" ><span style=\"margin-left: 10px;\">{1}</span></div>";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MenuBinding();
            }
        }

        private void MenuBinding()
        {
            if (UserLoginInfo.IsAdministrator())
            {
                T_MENU_BLL bll = new T_MENU_BLL();
                List<T_MENU_Entity> list = bll.GetList("", " SORTODER desc, MENUID asc ");
                RecursiveTreeBinding(list, "0", pContent);
            }
            else
            {
                UserLoginInfo loginInfo = UserLoginInfo.GetLoginInfo();
                List<V_ROLEMENU_Entity> list = loginInfo.RoleMenuList;
                RecursiveTreeBinding(list, "0", pContent);
            }
        }

        private void RecursiveTreeBinding(List<T_MENU_Entity> list, string parentID, Panel p)
        {
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td;
            Label lblHeight;
            Label lblMessage;
            StringBuilder sb = new StringBuilder();
            List<T_MENU_Entity> subList = list.FindAll(delegate(T_MENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (T_MENU_Entity entity in subList)
            {
                if (entity.MENUTYPE.ToString() == ((int)MenuType.目录).ToString())
                {
                    td = new HtmlTableCell();
                    td.Attributes.Add("class", "P");
                    td.Attributes.Add("onmouseover", string.Format("MO(this); popmsg(this.offsetLeft,this.offsetTop, {0});", entity.MENUID.ToString()));
                    td.Attributes.Add("onmouseout", "MU(this)");
                    td.Attributes.Add("onclick", string.Format("MO(this); popmsg(this.offsetLeft,this.offsetTop, {0});", entity.MENUID.ToString()));
                    td.Attributes.Add("id", string.Format("tdMenu{0}", entity.MENUID.ToString()));
                    td.InnerText = string.Format("　{0}　 ", entity.MENUNAME);
                    tr.Controls.Add(td);
                    List<T_MENU_Entity> pageList = list.FindAll(delegate(T_MENU_Entity en) { if (en.PARENTMENUID.ToString() == entity.MENUID.ToString() && en.MENUTYPE.ToString() == ((int)MenuType.页面).ToString()) return true; else return false; });
                    lblHeight = new Label();
                    lblHeight.Text = (pageList.Count * 25 + 4).ToString();
                    lblHeight.ID = "popHeight" + entity.MENUID.ToString();
                    lblHeight.CssClass = "popHeight" + entity.MENUID.ToString();
                    pContent.Controls.Add(lblHeight);
                    lblMessage = new Label();
                    lblMessage.ID = "popMsg" + entity.MENUID.ToString();
                    lblMessage.CssClass = "popMsg" + entity.MENUID.ToString();
                    sb = new StringBuilder();
                    sb.Append(String.Format(divBegin, (pageList.Count * 25 + 4).ToString()));
                    foreach (T_MENU_Entity en in pageList)
                    {
                        string url = en.PAGEPATH.TrimEnd('/') + "/" + en.PAGENAME;
                        if (en.MENUTARGET.ToLower() == "_blank")
                        {
                            sb.Append(string.Format(divRowToNewPage, url, en.MENUNAME));
                        }
                        else
                        {
                            sb.Append(string.Format(divRowToFrame, url, en.MENUNAME));
                        }
                    }
                    sb.Append(divEnd);
                    lblMessage.Text = sb.ToString();
                    pContent.Controls.Add(lblMessage);
                }
            }
            tMenu.Controls.Add(tr);
        }

        private void RecursiveTreeBinding(List<V_ROLEMENU_Entity> list, string parentID, Panel p)
        {
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td;
            Label lblHeight;
            Label lblMessage;
            StringBuilder sb = new StringBuilder();
            List<V_ROLEMENU_Entity> subList = list.FindAll(delegate(V_ROLEMENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (V_ROLEMENU_Entity entity in subList)
            {
                if (entity.MENUTYPE.ToString() == ((int)MenuType.目录).ToString())
                {
                    td = new HtmlTableCell();
                    td.Attributes.Add("class", "P");
                    td.Attributes.Add("onmouseover", string.Format("MO(this); popmsg(this.offsetLeft,this.offsetTop, {0});", entity.MENUID.ToString()));
                    td.Attributes.Add("onmouseout", "MU(this)");
                    td.Attributes.Add("onclick", string.Format("MO(this); popmsg(this.offsetLeft,this.offsetTop, {0});", entity.MENUID.ToString()));
                    td.Attributes.Add("id", string.Format("tdMenu{0}", entity.MENUID.ToString()));
                    td.InnerText = string.Format("　{0}　 ", entity.MENUNAME);
                    tr.Controls.Add(td);
                    List<V_ROLEMENU_Entity> pageList = list.FindAll(delegate(V_ROLEMENU_Entity en) { if (en.PARENTMENUID.ToString() == entity.MENUID.ToString() && en.MENUTYPE.ToString() == ((int)MenuType.页面).ToString()) return true; else return false; });
                    lblHeight = new Label();
                    lblHeight.Text = (pageList.Count * 25 + 4).ToString();
                    lblHeight.ID = "popHeight" + entity.MENUID.ToString();
                    lblHeight.CssClass = "popHeight" + entity.MENUID.ToString();
                    pContent.Controls.Add(lblHeight);
                    lblMessage = new Label();
                    lblMessage.ID = "popMsg" + entity.MENUID.ToString();
                    lblMessage.CssClass = "popMsg" + entity.MENUID.ToString();
                    sb = new StringBuilder();
                    sb.Append(String.Format(divBegin, (pageList.Count * 25 + 4).ToString()));
                    foreach (V_ROLEMENU_Entity en in pageList)
                    {
                        string url = en.PAGEPATH.TrimEnd('/') + "/" + en.PAGENAME;
                        if (en.MENUTARGET.ToLower() == "_blank")
                        {
                            sb.Append(string.Format(divRowToNewPage, url, en.MENUNAME));
                        }
                        else
                        {
                            sb.Append(string.Format(divRowToFrame, url, en.MENUNAME));
                        }
                    }
                    sb.Append(divEnd);
                    lblMessage.Text = sb.ToString();
                    pContent.Controls.Add(lblMessage);
                }
            }
            tMenu.Controls.Add(tr);
        }

    }
}
